from __future__ import with_statement
from pymt import *
from glob import glob
from OpenGL.GL import GL_LINE_STRIP, glColor4f, glVertex2f, GL_LINE_BIT, glLineWidth
from OpenGL.GL import glTranslatef
import math
import os, sys
import json
import urllib2

#if __name__ == '__main__':
class movieCoverFlow(MTCoverFlow):
    def __init__(self,**kwargs):
        super(movieCoverFlow, self).__init__(**kwargs)
        self.cover_angle=45
        self.thumbnail_size=(350,500)
        self.cover_distance=300
        self.pos = (200,100)
        self.size = (800,600)

        url = "http://192.168.0.2:8080"
        jsonrpc = "/jsonrpc"
        jsonrpcurl = url + jsonrpc
        requete = json.dumps({"jsonrpc":"2.0","method":"VideoLibrary.GetMovies","params":{"fields":["title"]},"id":1})
        resultat = urllib2.urlopen(jsonrpcurl,requete).read()
        result = json.loads(resultat);
        
        size = len(result['result']['movies'])
        #w = getWindow()
        #w.wallpaper ="Darkness1680.jpg"
        
        #coverflow = MTCoverFlow(cover_angle=45,thumbnail_size=(350,500),cover_distance=300)
        
        for i in range(0,size):
            film = result['result']['movies'][i]
        
            if film.has_key('thumbnail'):
                cible = url + "/thumb/" + film['thumbnail'] + ".jpg"
                filename = film['thumbnail'].split("/")
                dest = "cache/video/film/" + filename[len(filename)-1] + ".jpg"
                
                img = urllib2.urlopen(cible).read()
                local_file = open(dest , "wb")
                local_file.write(img)
                local_file.close()
                button = MTImageButton(image=Loader.image(dest))
        
            if film.has_key('title'):
                button.title = film['title']
            else:
                button.title = "Pas de titre"
                
            self.add_widget(button)
            #coverflow.add_widget(button)
        
        #runTouchApp(coverflow)
